﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Images;
using System.Drawing;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control to be used by <see cref="T:TextBox"/> to display the tooltip to access clipboard.
    /// </summary>
    public class DatePickerToolTip : ToolTipBase
    {
        const int buttonSpace = 1;
        const int buttonSize = 40;
        private DateTime SelectedDate;
        private TextBox textBox;
        private Label MonthLabel;
        private List<Control> buttons = new List<Control>(3);

        public DatePickerToolTip()
            : base()
        {
        }

        protected internal override IList<Control> SubControls { get { return buttons; } }

        protected override void OnLayout()
        {
            base.OnLayout();
        }

        private void buttonlayout()
        {
            //if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Appearance))
            //{
                int x = 0;
                int w = buttonSize, h = buttonSize;
                int y = Position == ToolTipPosition.Above ? 8 : 8 + 16;
                foreach (Control c in buttons)
                {
                    if (buttons.IndexOf(c) == 0)
                    {
                        Button b = c as Button;
                        b.Bounds = new Rectangle(16 + x, y, w * 2, h * 2);
                        x += w * 2 + buttonSpace;
                    }
                    else if (buttons.IndexOf(c) == 1)
                    {
                        Label b = c as Label;
                        b.Bounds = new Rectangle(16 + x, y, w * 6, h * 2);
                        x += w * 6 + buttonSpace;
                    }
                    else if (buttons.IndexOf(c) == 2)
                    {
                        Button b = c as Button;
                        b.Bounds = new Rectangle(16 + x, y, w * 2, h * 2);
                        y += buttonSize * 2 + buttonSpace;
                        x = 0;
                    }
                    else
                    {
                        Button b = c as Button;
                        if (b.Text == "11")
                        {
                            y += buttonSize + buttonSpace;
                            x = 0;
                        }
                        if (b.Text == "21")
                        {
                            y += buttonSize + buttonSpace;
                            x = 0;
                        }
                        if (b.Text == "31")
                        {
                            y += buttonSize + buttonSpace;
                            x = 0;
                        }
                        b.Bounds = new Rectangle(16 + x, y, w, h);
                        x += w + buttonSpace;
                    }

                }
            //}
        }

        public override void Show(Control anchor)
        {
            if (!Visible && Enabled)
            {
            textBox = anchor as TextBox;
            
            try
            {
                if (textBox.Text != string.Empty)
                {
                    SelectedDate = Convert.ToDateTime(textBox.Text);
                }
                else
                {
                    SelectedDate = DateTime.Now;
                    textBox.Text = SelectedDate.ToShortDateString();
                }
            }
                
            catch 
            {
                textBox.Text = SelectedDate.ToShortDateString();
                textBox.Text = SelectedDate.ToShortDateString();
            }
            Alpha = 220;
            base.Show(anchor);
            buttons = new List<Control>();
            SetupMonthButtons();
            SetupDayButtons();

            SetSize();
            buttonlayout();
           
            
        }
        }

        private void SetupMonthButtons()
        {
            Button DecreaseMonthButton = new Button
            {
                Text = "<-"
                ,
                Width = buttonSize * 2
                ,
                Height = buttonSize * 2
            };
            buttons.Add(DecreaseMonthButton);
            DecreaseMonthButton.Click += new EventHandler(OnMonthDecrease);
            MonthLabel = new Label
            {
                Text = SelectedDate.ToString("MMMM-yyyy")
                ,
                Alignment = Silvermoon.OpenGL.Alignment.Center,

                Width = buttonSize * 6
                ,
                Height = buttonSize * 2
            };
            buttons.Add(MonthLabel);
            Button IncreaseMonthButton = new Button
            {
                Text = "->"
                ,
                Width = buttonSize * 2
                ,
                Height = buttonSize * 2
            };
            buttons.Add(IncreaseMonthButton);
            IncreaseMonthButton.Click += new EventHandler(OnMonthIncrease);
        }

        private void SetupDayButtons()
        {
           int DaysInMonth = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.Calendar.GetDaysInMonth(SelectedDate.Year, SelectedDate.Month);

            for (int i = 1; i <= 31; i++)
            {
                Button oButton = new Button
                {
                    Text = i.ToString()
                    ,
                    Width = buttonSize
                    ,
                    Height = buttonSize
                };
                if (i > DaysInMonth)
                {
                    oButton.Visible = false;
                }
                oButton.Click += new EventHandler(OnDatePicked);
                buttons.Add(oButton);
            }
        }

        private void SetSize()
        {
            if (buttons[33].Visible == true)
            {
                Size = new Size(buttonSize * 10 + buttonSpace * 9 + 32, buttonSize * 6 + 30);
            }
            else
            {
                Size = new Size(buttonSize * 10 + buttonSpace * 9 + 32, buttonSize * 5 + 30);
            }
        }

        void OnDatePicked(object sender, EventArgs e)
        {
            Button b = sender as Button;
            string[] selecteddatestringArray = SelectedDate.ToShortDateString().Split('/');
           selecteddatestringArray[0]  = b.Text;
           string selecteddatestring = string.Join("/", selecteddatestringArray);
           SelectedDate = Convert.ToDateTime(selecteddatestring);
           textBox.Text = SelectedDate.ToShortDateString();
           Visible = false;
           
        }

        void OnMonthIncrease(object sender, EventArgs e)
        {
            SelectedDate = SelectedDate.AddMonths(1);
            MonthLabel.Text = SelectedDate.ToString("MMMM-yyyy");
            Button b = sender as Button;
            textBox.Text = SelectedDate.ToShortDateString();
            
            buttons.Clear();
            SetupMonthButtons();
            SetupDayButtons();
            SetSize();
            buttonlayout();
        }

        void OnMonthDecrease(object sender, EventArgs e)
        {
            SelectedDate = SelectedDate.AddMonths(-1);
            MonthLabel.Text = SelectedDate.ToString("MMMM-yyyy");
            Button b = sender as Button;
            textBox.Text = SelectedDate.ToShortDateString();

            buttons.Clear();
            SetupMonthButtons();
            SetupDayButtons();
            SetSize();
            buttonlayout();
        }
        
    }
}
